package com.zhangxin.aiInterview.manager.disruptor.strategy;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.zhangxin.aiInterview.manager.work_flow.ai_hr_interviewer.AiHrInterviewerAgentFacade;
import com.zhangxin.aiInterview.manager.work_flow.ai_tec_interviewer.AiTecInterviewerAgentFacade;
import com.zhangxin.aiInterview.manager.disruptor.WsEvent;
import com.zhangxin.aiInterview.manager.disruptor.WsEventProducer;
import com.zhangxin.aiInterview.manager.redis.stress_detector.StressDetectionConsumer;
import com.zhangxin.aiInterview.manager.wsserver.WsManager;
import com.zhangxin.aiInterview.manager.wsserver.enums.WsMessageTypeEnum;
import com.zhangxin.aiInterview.manager.wsserver.model.WsResponseMessage;
import com.zhangxin.aiInterview.model.enums.InterviewTypeEnum;
import com.zhangxin.aiInterview.utils.StressManager;
import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.web.socket.TextMessage;
import org.springframework.web.socket.WebSocketSession;

import javax.annotation.Resource;
import java.io.IOException;
import java.util.Objects;

@Service
@Slf4j
public class LLmByAgentSendMsg implements WsSendMsg {
    @Resource
    private AiTecInterviewerAgentFacade aiTecInterviewerAgentFacade;
    @Resource
    private AiHrInterviewerAgentFacade aiHrInterviewerAgentFacade;
    @Resource
    @Lazy
    private WsEventProducer wsEventProducer;
    @Resource
    private StressDetectionConsumer stressDetectionConsumer;
    @Resource
    private StressManager stressManager;

    @Override
    public void sendMessage(WsEvent wsEvent) {
        try {
            Long userId = wsEvent.getUserId();
            String userContent = wsEvent.getUserContent();
            String gender = wsEvent.getGender();
            Integer interviewType = wsEvent.getInterviewType();
            System.out.println("interviewType:" + interviewType);

            //1. 查看用户当前问题是否说谎
            String P = stressDetectionConsumer.startConsuming(userId, interviewType);
            boolean isLie = false;
            if (StrUtil.isNotBlank(P)) {
                //判断是否说谎
                isLie = Double.parseDouble(P) > 0.75;
            }

            //将压力值存储全局列表中
            stressManager.addStress(userId, P);
            log.info("用户压力值：{}", P);

            JSONObject jsonObject = new JSONObject();
            //构造用户回答
            jsonObject.set("userContent", userContent);
            jsonObject.set("isLie", isLie);

            String jsonStr = jsonObject.toString();
            System.out.println("用户回答：" + jsonStr);
            //2. 调用AI面试官智能体 获取面试问题文本
            String aiQuestion;
            if (Objects.equals(InterviewTypeEnum.TEC_INTERVIEW.getValue(), interviewType)) {
                //技术面
                aiQuestion = aiTecInterviewerAgentFacade.sendAndGetReply(userId, jsonStr);
            } else {
                //hr面
                aiQuestion = aiHrInterviewerAgentFacade.sendAndGetReply(userId, jsonStr);
            }

            System.out.println("面试官提问：" + aiQuestion);

//            //3. 异步tts 文本转语音
//            wsEventProducer.publishEvent(userId, aiQuestion, gender);
            //获取客户端ws连接
            WebSocketSession session = WsManager.getConnection(userId);

            //4. 返回问题的音频对应的文本信息给客户端
            WsResponseMessage wsResponseMessage = new WsResponseMessage(WsMessageTypeEnum.AUDIO_TEXT.getValue(), aiQuestion);
            session.sendMessage(new TextMessage(JSONUtil.toJsonStr(wsResponseMessage)));
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}
